﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using Anata;
using Anata.AnataServerDataSetTableAdapters;

namespace Anata.TableForm
{
    public partial class KaryawanForm : Form
    {
        AdminDialogueForm _f;
        
        karyawanTableAdapter karyawan = new karyawanTableAdapter();
        AnataServerDataSet.karyawanDataTable dt = new AnataServerDataSet.karyawanDataTable();

        Log Log = new Log();
        _log l;
        List<_log> list = new List<_log>();

        DataRowCollection dr;
        DataRow drow;

        bool isUpdate = false;
        int id;

        public KaryawanForm() { }

        public KaryawanForm(AdminDialogueForm f)
        {
            InitializeComponent();
            _f = f;

            dteTglLahir.Format = DateTimePickerFormat.Custom;
            dteTglLahir.CustomFormat = "yyyy-MM-dd";
            dteTglMasuk.Format = DateTimePickerFormat.Custom;
            dteTglMasuk.CustomFormat = "yyyy-MM-dd";
            dteTglKeluar.Format = DateTimePickerFormat.Custom;
            dteTglKeluar.CustomFormat = "yyyy-MM-dd";
            cmbGender.SelectedIndex = 0;

            this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.Form_FormClosing);
        }

        public KaryawanForm(AdminDialogueForm f, DataGridView dataGridView1)
        {
            InitializeComponent();

            dteTglLahir.Format = DateTimePickerFormat.Custom;
            dteTglLahir.CustomFormat = "yyyy-MM-dd";
            dteTglMasuk.Format = DateTimePickerFormat.Custom;
            dteTglMasuk.CustomFormat = "yyyy-MM-dd";
            dteTglKeluar.Format = DateTimePickerFormat.Custom;
            dteTglKeluar.CustomFormat = "yyyy-MM-dd";

            _f = f;
            isUpdate = true;
            id = Convert.ToInt32(dataGridView1[0, dataGridView1.SelectedCells[0].RowIndex].Value);

            dt = karyawan.GetDataByID(id);
            dr = dt.Rows;
            drow = dr[0];

            txtNama.Text = drow[dt.namaColumn].ToString();
            rtbAlamat.Text = drow[dt.alamatColumn].ToString();
            txtNoTelp.Text = drow[dt.no_telpColumn].ToString();
            txtNoKTP.Text = drow[dt.no_ktpColumn].ToString();
            if (drow[dt.is_maleColumn].ToString().Equals("1")) cmbGender.SelectedIndex = 0; else cmbGender.SelectedIndex = 1;
            dteTglLahir.Value = DateTime.Parse(drow[dt.tgl_lahirColumn].ToString());
            cmbStatus.SelectedValue = drow[dt.id_statusColumn].ToString();
            cmbAgama.SelectedValue = drow[dt.id_agamaColumn].ToString();
            cmbGolDar.SelectedValue = drow[dt.id_golongan_darahColumn].ToString();
            dteTglMasuk.Value = DateTime.Parse(drow[dt.tgl_masukColumn].ToString());
            dteTglKeluar.Value = DateTime.Parse(drow[dt.tgl_keluarColumn].ToString());
            cmbCabang.SelectedValue = drow[dt.id_cabangColumn].ToString();

            this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.Form_FormClosing);
        }

        private void Form_FormClosing(object sender, FormClosingEventArgs e)
        {
            _f.PerformRefresh();
        }

        private void cancelButton_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }

        private void insertData_Click(object sender, EventArgs e)
        {
            if (isDataValid())
            {
                if (isUpdate)
                {
                    if (txtNama.Modified)
                    {
                        l = new _log("karyawan", id, "nama", "edit", drow[dt.namaColumn].ToString(), txtNama.Text);
                        list.Add(l);
                    }
                    if (txtNoKTP.Modified)
                    {
                        l = new _log("karyawan", id, "no_ktp", "edit", drow[dt.no_ktpColumn].ToString(), txtNoKTP.Text);
                        list.Add(l);
                    }
                    if (txtNoTelp.Modified)
                    {
                        l = new _log("karyawan", id, "no_telp", "edit", drow[dt.no_telpColumn].ToString(), txtNoTelp.Text);
                        list.Add(l);
                    }
                    if (rtbAlamat.Modified)
                    {
                        l = new _log("karyawan", id, "alamat", "edit", drow[dt.alamatColumn].ToString(), rtbAlamat.Text);
                        list.Add(l);
                    }
                    if (cmbAgama.DroppedDown)
                    {
                        l = new _log("karyawan", id, "id_agama", "edit", drow[dt.id_agamaColumn].ToString(), cmbAgama.SelectedValue.ToString());
                        list.Add(l);
                    }
                    if (cmbGender.DroppedDown)
                    {
                        l = new _log("karyawan", id, "is_male", "edit", drow[dt.is_maleColumn].ToString(), cmbGender.SelectedIndex == 0 ? "1" : "0");
                        list.Add(l);
                    }
                    if (cmbGolDar.DroppedDown)
                    {
                        l = new _log("karyawan", id, "id_golongan_darah", "edit", drow[dt.id_golongan_darahColumn].ToString(), cmbGolDar.SelectedValue.ToString());
                        list.Add(l);
                    }
                    if (cmbStatus.DroppedDown)
                    {
                        l = new _log("agama", id, "id_status", "edit", drow[dt.id_statusColumn].ToString(), cmbStatus.SelectedValue.ToString());
                        list.Add(l);
                    }
                    if (cmbCabang.DroppedDown)
                    {
                        l = new _log("agama", id, "id_cabang", "edit", drow[dt.id_cabangColumn].ToString(), cmbCabang.SelectedValue.ToString());
                        list.Add(l);
                    }
                    if (!dteTglLahir.Text.Equals(drow[dt.tgl_lahirColumn].ToString()))
                    {
                        l = new _log("agama", id, "tgl_lahir", "edit", drow[dt.tgl_lahirColumn].ToString(), dteTglLahir.Text);
                        list.Add(l);
                    }
                    if (!dteTglMasuk.Text.Equals(drow[dt.tgl_masukColumn].ToString()))
                    {
                        l = new _log("agama", id, "tgl_masuk", "edit", drow[dt.tgl_masukColumn].ToString(), dteTglMasuk.Text);
                        list.Add(l);
                    }
                    if (!dteTglKeluar.Text.Equals(drow[dt.tgl_keluarColumn].ToString()))
                    {
                        l = new _log("agama", id, "tgl_keluar", "edit", drow[dt.tgl_keluarColumn].ToString(), dteTglKeluar.Text);
                        list.Add(l);
                    }
                    try
                    {
                        karyawan.UpdateQuery(txtNama.Text,
                            Convert.ToInt32(cmbCabang.SelectedValue),
                            rtbAlamat.Text,
                            txtNoTelp.Text,
                            txtNoKTP.Text,
                            cmbGender.SelectedIndex == 0 ? "true" : "false",
                            DateTime.Parse(dteTglLahir.Text),
                            Convert.ToInt32(cmbStatus.SelectedValue),
                            Convert.ToInt32(cmbAgama.SelectedValue),
                            Convert.ToInt32(cmbGolDar.SelectedValue),
                            DateTime.Parse(dteTglMasuk.Text),
                            DateTime.Parse(dteTglKeluar.Text), 
                            id);
                    }
                    catch (Exception msg) { MessageBox.Show("Update tidak berhasil, periksa kembali input yang anda masukkan."); }
                    this.Close();
                }
                else
                {
                    try
                    {
                        karyawan.Insert(txtNama.Text,
                           Convert.ToInt32(cmbCabang.SelectedValue),
                           rtbAlamat.Text,
                           txtNoTelp.Text,
                           txtNoKTP.Text,
                           cmbGender.SelectedIndex == 0 ? "true" : "false",
                           DateTime.Parse(dteTglLahir.Text),
                           Convert.ToInt32(cmbStatus.SelectedValue),
                           Convert.ToInt32(cmbAgama.SelectedValue),
                           Convert.ToInt32(cmbGolDar.SelectedValue),
                           DateTime.Parse(dteTglMasuk.Text),
                           DateTime.Parse(dteTglKeluar.Text));
                    }
                    catch (Exception msg) { MessageBox.Show("Insert tidak berhasil, periksa kembali input yang anda masukkan."); }
                    this.Close();
                }
                Log.ProcessLog(list);
            }
            else { MessageBox.Show("Data yang diperlukan belum lengkap."); }            
        }

        public bool isDataValid()
        {
            if (!txtNama.Text.Equals("") &&
                !rtbAlamat.Text.Equals("") &&
                !txtNoTelp.Text.Equals("") &&
                !dteTglLahir.Text.Equals("") &&
                !dteTglMasuk.Text.Equals("") &&
                !txtNoKTP.Text.Equals(""))
            { return true; }
            else { return false; }
        }

        private void KaryawanForm_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'anataServerDatabase.golongan_darah' table. You can move, or remove it, as needed.
            this.golongan_darahTableAdapter.Fill(this.anataServerDatabase.golongan_darah);
            // TODO: This line of code loads data into the 'anataServerDatabase.cabang' table. You can move, or remove it, as needed.
            this.cabangTableAdapter.Fill(this.anataServerDatabase.cabang);
            // TODO: This line of code loads data into the 'anataServerDatabase.agama' table. You can move, or remove it, as needed.
            this.agamaTableAdapter.Fill(this.anataServerDatabase.agama);
            // TODO: This line of code loads data into the 'anataServerDatabase.status' table. You can move, or remove it, as needed.
            this.statusTableAdapter.Fill(this.anataServerDatabase.status);

        }

        private void txtNoKTP_Validating(object sender, CancelEventArgs e)
        {
            if (txtNoKTP.ValidateControl() == false)
                errorProvider1.SetError(txtNoKTP, "Gunakan format KTP (xx.xx.xx.xxxxxx.xxxx)");
            else
                errorProvider1.SetError(txtNoKTP, "");
        }

    }
}
